package com.meshnetics.gb.type.impl.zcl;

import com.meshnetics.gb.type.DataType;
import com.meshnetics.gb.type.DataValue;
import com.meshnetics.gb.type.MandatoryFormatter;

/**
 * Abstract implementation of mandatory formatter. Defines log string based on representing value as serialized string
 * with additional info on type (and class for max log string).
 * <br>
 * Intended for extending to define serialization/deserializetion to/from string.
 *
 * @author AKovylin
 */
public abstract class AbstractMandatoryFormatter extends MandatoryFormatter {
    public AbstractMandatoryFormatter(DataType dataType) {
        super(dataType);
    }

    public String formatShortLogString(DataValue value) {
        checkValue(value);
        return value.getType().getName() + '(' + formatSerializedString(value) + ')';
    }

    public String formatMaxLogString(DataValue value) {
        checkValue(value);
        return value.getClass() + "(typeClass=" + value.getType().getClass() +
                ",typeName=" + value.getType().getName() + ",value=" + formatSerializedString(value) + ')';
    }

    protected void checkValue(DataValue value) {
        if (value == null) {
            throw new NullPointerException("value is null");
        }
        if (value.getType().getClass() != dataType.getClass()) {
            throw new IllegalArgumentException("unsupported value class " + value.getClass());
        }
    }
}
